package com.siashan.unipay4j.ali.pay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import com.siashan.unipay4j.core.IPayConfig;
import com.siashan.unipay4j.core.Result;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 支付宝付款码支付
 *
 * 当面付-付款码支付-付款码支付接口（统一收单交易支付接口）
 *
 * 商家使用扫码枪等条码识别设备，扫描用户支付宝钱包上的 条码/二维码，完成收款。用户仅需出示付款码，所有收款操作由商家端完成
 *
 * @author siashan
 * @since v1.0.1
 **/
@Slf4j
public class AliPayMICRO extends AliPay {


    /**
     * 销售产品码，商家和支付宝签约的产品码
     */
    private static final String PRODUCT_CODE = "FACE_TO_FACE_PAYMENT";


    public AliPayMICRO(IPayConfig aliPaySet) {
        super(aliPaySet);
    }

    public AliPayMICRO() {
        super();
    }


    /**
     * 支付
     *
     * @param params  支付参数
     * @return 支付结果
     */
    @Override
    public Result executePay(Map<String, Object> params) throws AlipayApiException {
        AlipayTradePayRequest request = new AlipayTradePayRequest();
        params.put("scene", "bar_code");
        params.put("product_code", PRODUCT_CODE);
        request.setBizContent(JSON.toJSONString(params));
        AlipayTradePayResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            return Result.tradeSuccess(response);
        } else {
            return Result.tradeFail(response.getSubCode(), response.getSubMsg());
        }
    }

    /**
     * 获取交易类型
     *
     * @return 交易类型
     */
    @Override
    public AliTradeTypeEnum getTradeType() {
        return AliTradeTypeEnum.MICROPAY;
    }


    @Override
    public boolean needBuyerId() {
        return false;
    }
}
